<?php


namespace app\service;


use app\Code;
use app\dao\HotelDao;
use app\dao\RolesDao;
use app\dao\UserDao;
use app\lib\exception\ServiceException;
use think\facade\Db;

class HotelService
{
    private $HotelDao;
    private $UserDao;
    private $UserService;
    private $FilesService;

    public function __construct()
    {
        $this->HotelDao = new HotelDao();
        $this->UserDao = new UserDao();
        $this->UserService = new UserService();
        $this->FilesService = new FilesService();
    }

    public function getHotel($id){
        return $this->HotelDao->customQuery()
            ->withRoomAndUser()
            ->filterById($id)
            ->getOne();
    }

    public function getHotelOnlyRoom($id){
        return $this->HotelDao->customQuery()
            ->withRoomAndRoomTypeAndRoomPrice()
            ->filterById($id)
            ->getOne();
    }

    public function getHotelList($page, $limit, $keyword, $province, $city){
        return $this->HotelDao->customQuery()
            ->fuzzyByName($keyword)
            ->filterByCity($city)
            ->filterByProvince($province)
            ->withRoomAndUser()
            ->getList($page , $limit);
    }

    public function getHotelAll($province , $city , $district , $page = 1 , $limit = 10){
        return $this->HotelDao->customQuery()
            ->filterByDistrict($district)
            ->filterByCity($city)
            ->filterByProvince($province)
            ->withRoomByStatus()
            ->Ban()
            ->getList($page , $limit);
    }

    public function editHotel($id, $data){
        $updateData = [];

        if (isset($data['name'])){
            if ($this->UserDao->customQuery()->filterByUsername($data['name'])->getCount() > 0) throw new ServiceException('酒店已存在');
            $updateData['name'] = $data['name'];
        }
        empty($data['province']) || $updateData['province'] = $data['province'];
        empty($data['city']) || $updateData['city'] = $data['city'];
        empty($data['district']) || $updateData['district'] = $data['district'];
        empty($data['address']) || $updateData['address'] = $data['address'];
        empty($data['phone']) || $updateData['phone'] = $data['phone'];
        empty($data['wechat']) || $updateData['wechat'] = $data['wechat'];
        if (isset($data['img_urls'])){
            $paths = $this->FilesService->save2Oss($data['img_urls'], 'hotel');
            $updateData['img_url'] = json_encode($paths);
        }

        if (isset($data['logo_url'])){
            $updateData['logo_url'] = $this->FilesService->save2Oss($data['logo_url'], 'hotel');
        }

        return $this->HotelDao->edit($id , $updateData);
    }

    public function addHotel($name , $province , $city , $district , $address , $linkman , $phone , $wechat , $img_urls , $logo_url){

        if ($this->UserDao->customQuery()
            ->filterByUsername($name)
            ->getCount() > 0 )
        {
            throw new ServiceException('酒店已存在');
        }

        try {
            Db::startTrans();

            $logo_url = $this->FilesService->save2Oss($logo_url, 'hotel');
            $paths = $this->FilesService->save2Oss($img_urls, 'hotel');
            $img_urls = json_encode($paths);

            $user = $this->UserService->addHotelAdmin($name, $logo_url);

            $hotel = $this->HotelDao->save([
                'name' => $name,
                'province' => $province,
                'city' => $city,
                'district' => $district,
                'address' => $address,
                'linkman' => $linkman,
                'phone' => $phone,
                'wechat' => $wechat,
                'user_id' => $user['id'],
                'logo_url' => $logo_url,
                'img_url' => $img_urls,
                'is_ban' => 0
            ]);

            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            throw new ServiceException($exception->getMessage());
        }

        $hotel['user'] = $user;
        return $hotel;
    }

    public function banHotel($is_ban, $id){
        if ($is_ban){
            return $this->HotelDao->offHotel($id);
        }else{
            return $this->HotelDao->onHotel($id);
        }
    }
}